package com.pkx.cloud.fse7.expand.plan.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pkx.cloud.fse7.expand.plan.entity.DoorPlan;
import com.pkx.cloud.fse7.expand.plan.entity.DoorPlanDetail;
import com.pkx.cloud.fse7.expand.plan.repo.DoorPlanMapper;
import com.pkx.cloud.fse7.expand.plan.service.DoorPlanDetailService;
import com.pkx.cloud.fse7.expand.plan.service.DoorPlanService;
import com.pkx.cloud.whzx.util.LoginUserUtil;
import org.apache.commons.compress.utils.Lists;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 服务实现类
 *
 * @author chenwg
 * @since 2021-04-09
 */
@Service
@DS("slave")
public class DoorPlanServiceImpl extends ServiceImpl<DoorPlanMapper, DoorPlan> implements DoorPlanService {

    @Resource
    private DoorPlanDetailService planDetailService;

    /**
     * 分页列表
     *
     * @param doorPlan
     * @return
     */
    @Override
    public Page<DoorPlan> pageList(Page page, DoorPlan doorPlan) {
        LambdaQueryWrapper<DoorPlan> lambdaQueryWrapper = Wrappers.lambdaQuery(DoorPlan.class);
        return page(page, lambdaQueryWrapper);
    }

    /**
     * 列表查询
     *
     * @param doorPlan
     * @return
     */
    @Override
    public List<DoorPlan> listByEntity(DoorPlan doorPlan) {
        LambdaQueryWrapper<DoorPlan> lambdaQueryWrapper = Wrappers.lambdaQuery(DoorPlan.class);

        return list(lambdaQueryWrapper);
    }

    /**
     * 详情
     *
     * @param id
     * @return
     */
    @Override
    public DoorPlan selectById(String id) {
        DoorPlanDetail doorPlanDetail = new DoorPlanDetail();
        doorPlanDetail.setPlanId(id);
        List<DoorPlanDetail> list = planDetailService.listByEntity(doorPlanDetail);
        DoorPlan byId = getById(id);
        byId.setGroupList(list);
        return byId;
    }

    /**
     * 保存
     *
     * @param doorPlan
     * @return
     */
    @Override
    public Boolean saveDoorPlan(DoorPlan doorPlan) {
        doorPlan.setCreateUserName(LoginUserUtil.getLoginUserName());
        save(doorPlan);
        List<DoorPlanDetail> detailIds = doorPlan.getGroupList();
        List<DoorPlanDetail> list = Lists.newArrayList();
        detailIds.forEach(detail -> {
            DoorPlanDetail doorPlanDetail = new DoorPlanDetail();
            doorPlanDetail.setGroupId(detail.getGroupId());
            doorPlanDetail.setGroupName(detail.getGroupName());
            doorPlanDetail.setPlanId(doorPlan.getId());
            list.add(doorPlanDetail);
        });
        return planDetailService.saveBatch(list);
    }

    /**
     * 修改
     *
     * @param doorPlan
     * @return
     */
    @Override
    public Boolean modify(DoorPlan doorPlan) {
        updateById(doorPlan);
        // 删除旧的关联数据
        planDetailService.deleteByPlantId(doorPlan.getId());

        List<DoorPlanDetail> detailIds = doorPlan.getGroupList();
        List<DoorPlanDetail> list = Lists.newArrayList();
        detailIds.forEach(detail -> {
            DoorPlanDetail doorPlanDetail = new DoorPlanDetail();
            doorPlanDetail.setGroupId(detail.getGroupId());
            doorPlanDetail.setGroupName(detail.getGroupName());
            doorPlanDetail.setPlanId(doorPlan.getId());
            doorPlanDetail.setGroupName(detail.getGroupName());
            list.add(doorPlanDetail);
        });
        return planDetailService.saveBatch(list);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Override
    public Boolean deleteById(String id) {
        removeById(id);
        return planDetailService.deleteByPlantId(id);
    }

    @Override
    public Boolean deleteByIds(List<String> ids) {
        removeByIds(ids);
        return planDetailService.deleteByPlantIds(ids);

    }
}
